/**
 * @(#)com.framework.web.sys.service.UserService
 *
 * Copyright (c) 2014-2018 上海天跃科技有限公司
 *
 * DESC:
 *
 */
package com.framework.web.sys.service;

import com.framework.core.constant.Globals;
import com.framework.core.easyui.domain.Tree;
import com.framework.web.sys.domain.Resource;
import com.framework.web.sys.domain.RoleResource;
import com.framework.web.sys.repository.ResourceRepository;
import com.framework.web.sys.vo.ResourceFastVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fusf
 * @version 1.0  2016/1/8
 */
@Service
public class ResourceService {

    @Autowired
    private ResourceRepository resourceRepository;

    /**
     * 新增或者保存
     *
     * @param resource
     * @return
     */
    public Resource save(Resource resource) {
        return resourceRepository.save(resource);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void delete(String id) {
        resourceRepository.delete(id);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public Resource findById(String id) {
        return resourceRepository.findOne(id);
    }

    /**
     * 获取资源列表信息
     *
     * @return
     */
    public List<Resource> findListOrderBySortIndex() {
        return resourceRepository.findAllOrderBySortIndex();
    }

    /**
     * 获取有效资源列表信息
     *
     * @return
     */
    public List<Resource> findListByStatusOrderBySortIndex() {
        return resourceRepository.findAllByStatusOrderBySortIndex();
    }


    /**
     * 获取资源树
     *
     * @param resourceList
     * @return
     */
    public List<Tree> findTreeResource(List<Resource> resourceList) {
        return this.findTreeResource(resourceList, null);
    }

    /**
     * 获取资源树(包含勾选)
     *
     * @param resourceList
     * @return
     */
    public List<Tree> findTreeResource(List<Resource> resourceList, List<RoleResource> roleResourceList) {
        List<Tree> treeList = new ArrayList<Tree>();
        for (Resource resource : resourceList) {//封装成树的格式
            Tree node = new Tree();
            node.setId(resource.getId());
            node.setText(resource.getResourceName());
            if (StringUtils.isNotEmpty(resource.getParentId())) {
                node.setParentId(resource.getParentId());
            }
            if (roleResourceList != null) {
                for (RoleResource rel : roleResourceList) {//遍历判断是否选中
                    if (resource.getId().equals(rel.getResourceId())) {
                        node.setChecked(true);
                    }
                }
            }
            Map<String, String> attributes = new HashMap<String, String>();
            attributes.put("url", resource.getResourceUrl());
            attributes.put("target", resource.getResourceDesc());
            node.setAttributes(attributes);
            treeList.add(node);
        }
        return Tree.getRoot(treeList);
    }

    /**
     * 快速生成配置资源
     * @param resource
     */
    @Transactional
    public void createFastResource(ResourceFastVo resource) {
        initMenu(resource);
    }


    /**
     * 菜单
     *
     * @param r
     * @return
     */
    private void initMenu(ResourceFastVo r) {
        Resource resource = new Resource();
        resource.setResourceType(Globals.RES_TYPE_MENU);
        resource.setResourceName(r.getResourceName());
        resource.setResourceUrl(r.getResourceUrl());
        resource.setStatus(Globals.STATUS_YES);
        resource.setSortIndex(r.getSortIndex());
        if (r.getParentId() != null && !"0".equals(r.getParentId())) {
            resource.setParentId(r.getParentId());
        }
        resource = resourceRepository.save(resource);
        if (resource != null) {
            initButtonAdd(resource.getId(),r);
            initButtonEdit(resource.getId(),r);
            initButtonRemove(resource.getId(),r);
        }
    }

    /**
     * 新增按钮
     *
     * @param parentId
     */
    private void initButtonAdd(String parentId, ResourceFastVo resourceFastVo) {
        Resource resource = new Resource();
        resource.setResourceType(Globals.RES_TYPE_AUTHORITY);
        resource.setResourceName(resourceFastVo.getName() + "新增");
        resource.setResourceUrl(resourceFastVo.getEntityName() + "_add");
        resource.setIconCls("icon-add");
        resource.setClickFun("sy_addFun('/" + resourceFastVo.getEntityName() + "/toSaveAndUpdate/','添加" + resourceFastVo.getName() + "信息')");
        resource.setStatus(Globals.STATUS_YES);
        resource.setSortIndex(1);
        resource.setParentId(parentId);
        resourceRepository.save(resource);
    }

    /**
     * 修改按钮
     *
     * @param parentId
     */
    private void initButtonEdit(String parentId, ResourceFastVo resourceFastVo) {
        Resource resource = new Resource();
        resource.setResourceType(Globals.RES_TYPE_AUTHORITY);
        resource.setResourceName(resourceFastVo.getName() + "修改");
        resource.setResourceUrl(resourceFastVo.getEntityName() + "_edit");
        resource.setIconCls("icon-edit");
        resource.setClickFun("sy_editFun('/" + resourceFastVo.getEntityName() + "/toSaveAndUpdate/','修改" + resourceFastVo.getName() + "信息')");
        resource.setStatus(Globals.STATUS_YES);
        resource.setSortIndex(2);
        resource.setParentId(parentId);
        resourceRepository.save(resource);
    }

    /**
     * 删除按钮
     *
     * @param parentId
     */
    private void initButtonRemove(String parentId, ResourceFastVo resourceFastVo) {
        Resource resource = new Resource();
        resource.setResourceType(Globals.RES_TYPE_AUTHORITY);
        resource.setResourceName(resourceFastVo.getName() + "删除");
        resource.setResourceUrl(resourceFastVo.getEntityName() + "_remove");
        resource.setIconCls("icon-remove");
        resource.setClickFun("sy_removeGridFun_tip('/" + resourceFastVo.getEntityName() + "/toSaveAndUpdate/','删除" + resourceFastVo.getName() + "信息')");
        resource.setStatus(Globals.STATUS_YES);
        resource.setSortIndex(3);
        resource.setParentId(parentId);
        resourceRepository.save(resource);
    }


}
